package ckw.database.model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import ckw.managers.ModulesManager;

@Entity
@Table(name = "electionNodes")
public class ElectionNode implements Serializable {
	private static final long serialVersionUID = -5066052229409150199L;
	@Id
	@Column(name = "id")
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id = null;
	@ManyToOne
	@JoinColumn(name = "node", insertable = false, updatable = true)
	private Node node;
	@ManyToOne
	@JoinColumn(name = "election", insertable = false, updatable = true)
	private Election election;
	@OneToMany
	@JoinColumn(name = "election")
	private Set<AdministratorTokenExtended> administratorTokens = new HashSet<AdministratorTokenExtended>();

	public Long getId() {
		return id;
	}

	@SuppressWarnings("unused")
	private void setId(Long id) {
		this.id = id;
	}

	public Node getNode() {
		return node;
	}

	public void setNode(Node node) {
		this.node = node;
	}

	public Election getElection() {
		return election;
	}

	public void setElection(Election election) {
		this.election = election;
	}

	public Set<AdministratorTokenExtended> getAdministratorTokens() {
		return administratorTokens;
	}

	public void setAdministratorTokens(Set<AdministratorTokenExtended> administratorTokens) {
		this.administratorTokens = administratorTokens;
	}
	
	public void removeNode() {
		this.node.getElections().remove(this);
		ModulesManager.getInstance().updateNode(this.node);
		setNode(null);
	}
}
